package com.etlmaster.executor.steps.datax;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.etlmaster.core.DataSourceDefine;
import com.etlmaster.core.DataSourceHolder;
import com.etlmaster.executor.utils.DataCollectionUtils;
import com.etlmaster.executor.utils.StepState;

/**
 * @author Wugf
 * @date 2016-03-08
 */
public class DbReaderProcessor {
	private String dsType;
	private String fplSufix="reader";
	private Map<String,String> buildReaderMeta(DbReaderStepMeta meta){
		DataSourceDefine def = DataSourceHolder.getInstance()
				.getDataSourceDefine(meta.getDsName());

		Map<String, String> data = new HashMap<String, String>();
		data.put("url", def.getUrl());
		data.put("username",def.getUsername());
		data.put("password",def.getPassword());
		data.put("sql", meta.getSql());
		data.put("concurrency", meta.getConcurrency()+"");
		return data;
	}
	
	public int process(DbReaderStepMeta meta){
		dsType=DataCollectionUtils.getDsType(
				meta.getDsName());
		if(isSupportedDb()){
			Map<String,String> readerMeta=buildReaderMeta(meta);
			return DataxReader.runDataxReader(this.hashCode(),dsType+fplSufix,readerMeta,meta);
		}else{
			meta.log("ERROR","不支持抽取的数据库类型:{}",dsType);
			return StepState.RUNNING_FAILED;
		}
	}
	public boolean isSupportedDb(){
		String[] dbArray=DataCollectionUtils.getProperties("dbreaderSupportDbList").split(",");
		List dblist=Arrays.asList(dbArray);
		return dblist.contains(dsType);
	}

}
